package com.jushu.storbox;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.actions.ShareActions;
import com.jushu.storbox.adapter.ShareFileSearchAdapter;
import com.jushu.storbox.adapter.ShareFileSearchUserAdapter;
import com.jushu.storbox.callback.IOneboxLastDisTimerListener;
import com.jushu.storbox.constant.ExceptionConstant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.SharedUser;
import com.jushu.storbox.handler.ExceptionHandler;
import com.jushu.storbox.handler.ShareExceptionHandler;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.network.WifiController;
import com.jushu.storbox.service.CloudDriveService;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.OneboxLastDisTimer;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.dialog.ClouddriveDialog;
import com.jushu.storbox.view.viewImpl.ResizeLinearLayout;
import com.jushu.storbox.view.viewImpl.ResizeLinearLayout.OnKbStateChangedListner;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.ShareInfo;
import com.huawei.sharedrive.sdk.android.modelV2.request.SharedUserV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.UserInfoV2;
import com.huawei.sharedrive.sdk.android.serviceV2.ShareClientV2;

@SuppressWarnings("deprecation")
public class ShareFileSearchActivity extends Activity implements OnClickListener, OnKbStateChangedListner {
	private final static String LOG_TAG = "ShareFileSearchActivity";
	private EditText searchEdittext;
	private ShareClientV2 shareClient;
	private Button nextButton;
	private ImageView cleanButton, searchButton;
	private ProgressBar progressBar;
	private ListView userListview;
	private ICloudDriveService shareDriveService;
	private FileFolderInfo fileInformation;
	private Gallery viewPager;
	private ShareFileSearchAdapter shareFileUserAdapter;
	private List<UserInfoV2> adUserList;
	private List<SharedUser> shareADuserList = null;
	private ShareFileSearchUserAdapter shareFileSearchUserAdapter;
	private ShareAutoCompleteAsyncTask autoCompleteAsyncTask;
	private View mBtnCancel;
	private Handler mHandler;
	private ShareActions shareAction;

	private OneboxLastDisTimer lastDisTimer = new OneboxLastDisTimer(800, 800);
	private ExceptionHandler exceptionHandler;

	private Handler serverErrorCodeHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ExceptionConstant.Resource_Code:
				Toast.makeText(ShareFileSearchActivity.this,
						ShareFileSearchActivity.this.getResources().getString(R.string.operation_file_has_been_changed_at_server), Toast.LENGTH_SHORT).show();

				break;

			default:
				super.handleMessage(msg);
			}
		}

	};

	private void handleServerErrorCode(ClientException e) {
		Message msg = new Message();
		msg.what = e.getStatusCode();
		msg.obj = e.getCode();
		serverErrorCodeHandler.sendMessage(msg);
	}

	/***
	 * seervice connection
	 */
	public ServiceConnection serviceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			LogUtil.i(LOG_TAG, "onServiceConnected()");
			shareDriveService = ((CloudDriveService.CenterShareDriveServiceServiceBinder) service).getService();
			if (null != shareDriveService) {
				exceptionHandler.setCloudDriveService(shareDriveService);
			} else {
				PublicTools.restartApp(ShareFileSearchActivity.this);
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			shareDriveService = null;
			LogUtil.e(LOG_TAG, "onServiceDisconnected()");
		}
	};
	private ResizeLinearLayout mParentLayout;
	private View mBottomLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share_file_search_layout);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG, ShareFileSearchActivity.this);
		adUserList = new ArrayList<UserInfoV2>();
		exceptionHandler = new ShareExceptionHandler(this);
		bindservice();
		initView();
		getShareUserData();
		initHandler();
		shareAction = new ShareActions(this);
	}

	private void initHandler() {
		mHandler = new Handler() {
			private int first;

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
					Log.i("niuxiang", first + "");
					Log.i("niuxiang", "0 run");
					if (first++ == 0) {
						Log.i("niuxiang", "0 run");
						mBottomLayout.setVisibility(View.VISIBLE);
					} else {
						Log.i("niuxiang", "1 run");
						mBottomLayout.setVisibility(View.GONE);
					}
					break;
				case 1:
					if (shareADuserList == null || shareADuserList.size() == 0) {
						mBottomLayout.setVisibility(View.GONE);
					} else {
						mBottomLayout.setVisibility(View.VISIBLE);
					}
				default:
					break;
				}
			}
		};
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != serviceConnection)
			unbindService(serviceConnection);
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
	}

	private void initView() {
		mParentLayout = (ResizeLinearLayout) findViewById(R.id.layout_parent);
		mBottomLayout = findViewById(R.id.share_file_searche_user_viewpage_layout);
		searchEdittext = (EditText) findViewById(R.id.share_file_search_edittext);
		searchEdittext.setHint(getResources().getString(R.string.share_file_please_enter));
		nextButton = (Button) findViewById(R.id.share_file_search_go_button);
		cleanButton = (ImageView) findViewById(R.id.share_file_clean_imageview);
		progressBar = (ProgressBar) findViewById(R.id.share_file_search_ProgressBar);
		searchButton = (ImageView) findViewById(R.id.share_file_search_imageview);
		userListview = (ListView) findViewById(R.id.share_file_search_listView);
		viewPager = (Gallery) findViewById(R.id.share_file_searche_user_viewpage);
		mBtnCancel = findViewById(R.id.tv_cancel);
		viewPager.setSpacing(5);
		SetOnListener();
	}

	private void SetOnListener() {
		searchEdittext.addTextChangedListener(new SearchEdittextChangedListener());
		userListview.setOnItemClickListener(new SearchEdittextOnItemListener());
		nextButton.setOnClickListener(new SetOnClickListener());
		cleanButton.setOnClickListener(new SetOnClickListener());
		mBtnCancel.setOnClickListener(this);
		mParentLayout.setOnKbStateChangedListner(this);

	}

	private class SetOnClickListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			if (arg0.equals(nextButton)) {
				confirmShareData();
			} else if (arg0.equals(cleanButton)) {
				searchEdittext.setText("");
				if (!ShareDriveApplication.getInstance().isFastDoubleClick()) {//
					if (null == adUserList) {
						adUserList = new ArrayList<UserInfoV2>();
					}
					adUserList.clear();
					//
					shareFileSearchUserAdapter = new ShareFileSearchUserAdapter(ShareFileSearchActivity.this, 0, adUserList);
					userListview.setAdapter(shareFileSearchUserAdapter);
					shareFileSearchUserAdapter.notifyDataSetChanged();
					//
				}
			}
		}
	}

	private class SearchEdittextChangedListener implements TextWatcher {
		private int search_text_size = 0;

		@Override
		public void afterTextChanged(final Editable arg0) {

			IOneboxLastDisTimerListener callback = new IOneboxLastDisTimerListener() {
				@Override
				public void onFinish() {
					if (arg0.length() > 0) {
						int struts = new WifiController(ShareFileSearchActivity.this).getNetWord();
						if (struts == 0) {
							Message message = new Message();
							message.arg1 = MessageCode.NETWORK_EXCEPTION;
							exceptionHandler.sendMessage(message);
						} else {
							if (autoCompleteAsyncTask != null) {
								autoCompleteAsyncTask = null;
							}
							progressBar.setVisibility(View.VISIBLE);
							searchButton.setVisibility(View.GONE);
							autoCompleteAsyncTask = new ShareAutoCompleteAsyncTask();
							autoCompleteAsyncTask.execute();
						}
					} else {
						progressBar.setVisibility(View.GONE);
						searchButton.setVisibility(View.VISIBLE);
					}

					if (arg0.length() > 0) {
						cleanButton.setVisibility(View.VISIBLE);
					} else {
						cleanButton.setVisibility(View.GONE);
					}
				}
			};
			//
			lastDisTimer.setCallback(callback);
			lastDisTimer.restartTimer();
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			search_text_size = arg2;
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

		}
	}

	private class ShareAutoCompleteAsyncTask extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected synchronized Integer doInBackground(Integer... arg0) {
			try {
				int struts = new WifiController(ShareFileSearchActivity.this).getNetWord();
				if (struts == 0) {
					Message message = new Message();
					message.arg1 = MessageCode.NETWORK_EXCEPTION;
					exceptionHandler.sendMessage(message);
					return 1;
				} else {
					adUserList = shareDriveService.queryMailList(searchEdittext.getText().toString());
					if (null != adUserList) {
						if (adUserList.size() > 0) {
							return 0;// get data not empty
						} else {
							adUserList.clear();
							return 1;// get data empty
						}
					} else {
						adUserList = new ArrayList<UserInfoV2>();
						adUserList.clear();
						return 1;
					}
				}
			} catch (ClientException e) {
				e.printStackTrace();
				if (null != adUserList) {
					adUserList.clear();
				} else {
					adUserList = new ArrayList<UserInfoV2>();
					adUserList.clear();
				}
				handleServerErrorCode(e);
				return 2;// server connect fail
			}
			// return 1;
			catch (Exception e) {
				e.printStackTrace();
				return 2;// server connect fail
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			try {
				if (result == 0)// has value
				{
					progressBar.setVisibility(View.GONE);
					searchButton.setVisibility(View.VISIBLE);
				} else if (result == 1)// has't value
				{
					progressBar.setVisibility(View.GONE);
					searchButton.setVisibility(View.VISIBLE);
					Message message = new Message();
					message.arg1 = MessageCode.RESULT_IS_EMPTY;
					exceptionHandler.sendMessage(message);
					cancel(true);
				} else if (result == 2) {// server fail
					if (null != exceptionHandler) {
						Message message = new Message();
						message.arg1 = MessageCode.SERVER_REQUEST_FAIL;
						exceptionHandler.sendMessage(message);
					}
				}
				List<String> adUserName = new ArrayList<String>();
				if (null == adUserList) {
					adUserList = new ArrayList<UserInfoV2>();
				}
				for (UserInfoV2 user : adUserList) {
					adUserName.add(user.getName() + user.getDescription());
				}
				shareFileSearchUserAdapter = new ShareFileSearchUserAdapter(ShareFileSearchActivity.this, 0, adUserList);
				userListview.setAdapter(shareFileSearchUserAdapter);
				shareFileSearchUserAdapter.notifyDataSetChanged();
				cancel(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private class SearchEdittextOnItemListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			if (!mBottomLayout.isShown()) {
				mBottomLayout.setVisibility(View.VISIBLE);
			}
			searchEdittext.setText("");
			if (shareADuserList == null) {
				shareADuserList = new ArrayList<SharedUser>();
			}
			if (shareFileUserAdapter == null) {
				shareFileUserAdapter = new ShareFileSearchAdapter(ShareFileSearchActivity.this, shareADuserList);
				viewPager.setAdapter(shareFileUserAdapter);
			}
			if (shareADuserList.size() > 0) {
				if (adUserList.size() > 0) {
					if (adUserList.get(arg2).getLoginName().toLowerCase().equals(PublicTools.getClientUserName(ShareFileSearchActivity.this).toLowerCase())) {
						Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.not_share_their_own),
								Toast.LENGTH_LONG).show();
						return;
					} else {
						boolean isAdd = false;
						for (int i = 0; i < shareADuserList.size(); i++) {
							if (shareADuserList.get(i).getSharedUserName().equals(adUserList.get(arg2).getName())) {
								isAdd = false;
								Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.user_has_add),
										Toast.LENGTH_LONG).show();
								return;
							} else {
								isAdd = true;
							}
						}
						if (isAdd) {
							SharedUser shareduser = newSharedUser(adUserList.get(arg2), fileInformation);
							shareADuserList.add(shareduser);
						}
					}
				} else {
					Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.add_shared_fail_ineffi),
							Toast.LENGTH_LONG).show();
				}

			} else {
				if (adUserList.size() > 0) {
					if (adUserList.get(arg2).getLoginName().toLowerCase().equals(PublicTools.getClientUserName(ShareFileSearchActivity.this).toLowerCase())) {
						Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.not_share_their_own),
								Toast.LENGTH_LONG).show();
						return;
					} else {
						SharedUser shareduser = newSharedUser(adUserList.get(arg2), fileInformation);
						shareADuserList.add(shareduser);
					}
				} else {
					Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.add_shared_fail_ineffi),
							Toast.LENGTH_LONG).show();
				}

			}
			searchEdittext.clearFocus();
			shareFileUserAdapter.notifyDataSetChanged();
			viewPager.setSelection(viewPager.getCount());
		}

		private SharedUser newSharedUser(UserInfoV2 userInfo, FileFolderInfo fileInformation) {
			SharedUser shareduser = new SharedUser();
			shareduser.setiNodeId(Long.valueOf(fileInformation.getId()));
			shareduser.setName(fileInformation.getName());
			shareduser.setShareStruts(false);
			shareduser.setSharedDepartment(userInfo.getDescription());
			shareduser.setSharedUserLoginName(userInfo.getLoginName());
			shareduser.setSharedUserId(userInfo.getCloudUserId());
			shareduser.setSharedUserName(userInfo.getName());
			shareduser.setSharedUserMail(userInfo.getEmail());
			return shareduser;
		}
	}

	public void shareListOperating(int itemID) {
		if (shareADuserList.get(itemID).isShareStruts()) {
			deleteSharedUser(itemID, fileInformation.getId(), Long.valueOf(shareADuserList.get(itemID).getSharedUserId()));
		} else {
			deleteSharedListUser(itemID, fileInformation.getId());
		}
	}

	public void deleteSharedListUser(final int itemID, String iNodeId) {
		final ClouddriveDialog delete = new ClouddriveDialog(ShareFileSearchActivity.this, R.style.dialog_upload, R.layout.dialog_filedelete);

		View conventView = delete.getConventView();
		Button dialog_cancal = (Button) conventView.findViewById(R.id.dialog_add_file_cancal_bt);

		Button dialog_OK = (Button) conventView.findViewById(R.id.dialog_add_file_ok_bt);
		TextView dialog_prompt_tv = (TextView) conventView.findViewById(R.id.dialog_prompt_tv);
		dialog_prompt_tv.setText(getString(R.string.clouddrive_shared_file_remove_user));
		dialog_cancal.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				delete.dismiss();
			}
		});
		dialog_OK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				delete.dismiss();
				for (int i = 0; i < shareADuserList.size(); i++) {
					if (i == itemID) {
						shareADuserList.remove(itemID);
						if (shareFileUserAdapter == null) {
							shareFileUserAdapter = new ShareFileSearchAdapter(ShareFileSearchActivity.this, shareADuserList);
						}
						shareFileUserAdapter.notifyDataSetChanged();
						break;
					}
				}
				if (shareADuserList.size() <= 0) {
					mBottomLayout.setVisibility(View.GONE);
				}
			}
		});
		delete.show();
	}

	public void deleteSharedUser(final int itemID, final String iNodeId, final long sharedUserId) {
		final ClouddriveDialog delete = new ClouddriveDialog(ShareFileSearchActivity.this, R.style.dialog_upload, R.layout.dialog_filedelete);

		View conventView = delete.getConventView();
		Button dialog_cancal = (Button) conventView.findViewById(R.id.dialog_add_file_cancal_bt);

		Button dialog_OK = (Button) conventView.findViewById(R.id.dialog_add_file_ok_bt);
		TextView dialog_prompt_tv = (TextView) conventView.findViewById(R.id.dialog_prompt_tv);
		dialog_prompt_tv.setText(getString(R.string.clouddrive_shared_file_cancel_shared));
		dialog_cancal.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				delete.dismiss();
			}
		});
		dialog_OK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				delete.dismiss();
				int struts = new WifiController(ShareFileSearchActivity.this).getNetWord();
				if (struts == 0) {
					Message message = new Message();
					message.arg1 = MessageCode.NETWORK_EXCEPTION;
					exceptionHandler.sendMessage(message);
				} else {
					new DeletehareUserAsyncTask(itemID, fileInformation.getId(), sharedUserId).execute();
				}
			}
		});
		delete.show();
	}

	private class DeletehareUserAsyncTask extends AsyncTask<Integer, Integer, Integer> {
		private int itemID;
		private String iNodeId;
		private long sharedUserId;
		private SharedUserV2 shareUser;

		public DeletehareUserAsyncTask(int itemID, String iNodeId, long sharedUserId) {
			this.itemID = itemID;
			this.iNodeId = iNodeId;
			this.sharedUserId = sharedUserId;
		}

		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (shareClient == null) {
				shareClient = ShareClientV2.getInstance();
			}
			try {
				LogUtil.i(LOG_TAG, "delete shared user data...");
				int struts = new WifiController(ShareFileSearchActivity.this).getNetWord();
				if (struts == 0) {
					Message message = new Message();
					message.arg1 = MessageCode.NETWORK_EXCEPTION;
					exceptionHandler.sendMessage(message);
					return 1;
				} else {
					shareUser = new SharedUserV2();
					shareUser.setId(String.valueOf(sharedUserId));
					shareClient.deleteShareShips(ShareDriveApplication.getInstance().getAuthorization(), shareUser, ShareDriveApplication.getInstance()
							.getWnerID(), iNodeId);
					for (int i = 0; i < shareADuserList.size(); i++) {
						if (shareADuserList.get(i).getSharedUserId().equals(sharedUserId)) {
							shareADuserList.remove(i);
						}
					}
					return 0;
				}
			} catch (ClientException e) {
				LogUtil.e(LOG_TAG, "[deleteSharedUser]code:" + e.getCode() + " | message:" + e.getLocalizedMessage());
				e.printStackTrace();
				handleServerErrorCode(e);
				return 1;
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			if (result == 0) {
				if (shareFileUserAdapter == null) {
					shareFileUserAdapter = new ShareFileSearchAdapter(ShareFileSearchActivity.this, shareADuserList);
				}
				shareFileUserAdapter.notifyDataSetChanged();
			}
		}
	}

	private void getShareUserData() {
		fileInformation = (FileFolderInfo) getIntent().getSerializableExtra("sharedNodeInfo");
		shareADuserList = (List<SharedUser>) getIntent().getSerializableExtra("userInfoList");
		if (shareADuserList == null) {
			shareADuserList = new ArrayList<SharedUser>();
		}
		shareFileUserAdapter = new ShareFileSearchAdapter(ShareFileSearchActivity.this, shareADuserList);
		viewPager.setAdapter(shareFileUserAdapter);
	}

	private void confirmShareData() {
		if (shareADuserList != null) {
			if (shareADuserList.size() > 0) {
				final List<SharedUserV2> shareUserList = new ArrayList<SharedUserV2>();
				for (int i = 0; i < shareADuserList.size(); i++) //
				{
					if (!shareADuserList.get(i).isShareStruts()) {
						SharedUserV2 iNodeShare = new SharedUserV2();
						iNodeShare.setId(String.valueOf(shareADuserList.get(i).getSharedUserId()));
						iNodeShare.setSharedUserLoginName(shareADuserList.get(i).getSharedUserLoginName());
						iNodeShare.setEmail(shareADuserList.get(i).getSharedUserMail());
						shareUserList.add(iNodeShare);
					}
				}
				if (shareUserList.size() > 0) {
					// putShareRequest.setShareList(iNodeShareList);//
					// putShareRequest.setMessage(message);
					final ClouddriveDialog shared = new ClouddriveDialog(ShareFileSearchActivity.this, R.style.dialog_upload,
							R.layout.share_file_message_layout);

					View conventView = shared.getConventView();
					Button dialog_message_cancal_bt = (Button) conventView.findViewById(R.id.dialog_message_cancal_bt);

					Button dialog_message_ok_bt = (Button) conventView.findViewById(R.id.dialog_message_ok_bt);

					final EditText messageED = (EditText) conventView.findViewById(R.id.share_file_message_edittext);
					WindowManager windowManager=getWindowManager();
					Display display=windowManager.getDefaultDisplay();
					shared.setWidth(display.getWidth());
					shared.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
					dialog_message_cancal_bt.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							shared.dismiss();
						}
					});
					dialog_message_ok_bt.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							// putShareRequest.setMessage(messageED.getText().toString());
							int struts = new WifiController(ShareFileSearchActivity.this).getNetWord();
							if (struts == 0) {
								Toast.makeText(ShareFileSearchActivity.this,
										ShareFileSearchActivity.this.getResources().getString(R.string.allfile_notwork_obstructed), Toast.LENGTH_LONG).show();
								return;
							} else {
								new AsyConfirmShareData(fileInformation, shareUserList, messageED.getText().toString()).execute();
							}
							shared.dismiss();
						}
					});
					shared.show();

				} else {
					Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.please_add_share),
							Toast.LENGTH_LONG).show();
				}
			} else {
				Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.please_add_share),
						Toast.LENGTH_LONG).show();
			}
		} else {
			Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.please_add_share), Toast.LENGTH_LONG)
					.show();
		}
	}

	private class AsyConfirmShareData extends AsyncTask<Integer, Integer, Integer> {
		FileFolderInfo fileInformation;
		private String message;
		private List<SharedUserV2> putShareRequest;

		public AsyConfirmShareData(FileFolderInfo fileInformation, List<SharedUserV2> putShareRequest, String message) {
			this.putShareRequest = putShareRequest;
			this.fileInformation = fileInformation;
			this.message = message;
		}

		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (shareClient == null) {
				shareClient = ShareClientV2.getInstance();
			}
			try {
				LogUtil.i(LOG_TAG, "add share data...");
				ShareInfo shareInfo = new ShareInfo();
				shareInfo.setOwnerId(ShareDriveApplication.getInstance().getWnerID());
				shareInfo.setOwnerName(PublicTools.getClientUserName(ShareFileSearchActivity.this));
				shareInfo.setNodeId(fileInformation.getId());
				shareInfo.setNodeName(fileInformation.getName());
				shareInfo.setMessage(message);
				shareAction.batchShare(shareDriveService, exceptionHandler, putShareRequest, shareInfo);
				return 0;
			} catch (ClientException e) {
				LogUtil.e(LOG_TAG, "[addShare]code:" + e.getCode() + " | message:" + e.getLocalizedMessage());
				e.printStackTrace();
				// handleServerErrorCode(e);
				Message message = new Message();
				message.what = e.getStatusCode();
				message.getData().putString("code", e.getCode());
				exceptionHandler.sendMessage(message);
				// if (e.getStatusCode() == ExceptionConfig.Resource_Code) {
				// Toast.makeText(
				// ShareFileSearchActivity.this,
				// ShareFileSearchActivity.this
				// .getResources()
				// .getString(
				// R.string.operation_file_has_been_changed_at_server),
				// Toast.LENGTH_SHORT).show();
				// } else {
				// Toast.makeText(
				// ShareFileSearchActivity.this,
				// ShareFileSearchActivity.this.getResources()
				// .getString(R.string.share_failed),
				// Toast.LENGTH_SHORT).show();
				// }
				return 1;
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			if (result == 0) {
				Toast.makeText(ShareFileSearchActivity.this, ShareFileSearchActivity.this.getResources().getString(R.string.share_success), Toast.LENGTH_SHORT)
						.show();
				ShareFileSearchActivity.this.finish();
			}
		}
	}

	/***
	 * bind service
	 */
	private void bindservice() {
		Intent intent = new Intent(ShareFileSearchActivity.this, CloudDriveService.class);
		intent.setAction("tokenAlarm");
		if (null != serviceConnection) {
			bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_cancel:
			onBackPressed();
			break;

		default:
			break;
		}

	}

	@Override
	public void onChanged(boolean in) {
		if (in) {
			// Log.i("liuxiang", "handler run" + in);
			mHandler.sendEmptyMessage(0);
		} else {
			mHandler.sendEmptyMessage(1);
		}

	}

}
